package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName TestController
 * @Description TODO
 * @Author 86181
 * @Date 2022/9/5 17:50
 * @Version 1.0
 **/
@Controller
@RequestMapping("test")
public class TestController {
    @RequestMapping("voidShow")
    public void voidShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("voidShow");
        request.setAttribute("message","dg");
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);

    }
    @RequestMapping("voidShow1")
    public void voidShow1(HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("voidShow1");
        response.sendRedirect("/view/success.jsp");
    }
    @RequestMapping("show2")
    public ModelAndView show2(){
        System.out.println("show2");
        //ModelAndView 在处理适配器执行完全之后回返回modelAndView 模型和视图 由视图进行解析
        //Model 封装了数据  view 视图
        ModelAndView modelAndView = new ModelAndView();
        //设置model    ModelMap 是继承自LindkedHashMap   key=value
        modelAndView.addObject("msg","steel");
        //设置视图
        modelAndView.setViewName("success");
        return modelAndView;
    }
    @RequestMapping(value = "show3")
    public String show3(Map<String,Object> map){
        map.put("name","dg");
       return "success";
    }
@RequestMapping("show4")
    public String show4(Model model){
        model.addAttribute("age",18);
        return "success";
    }
    @RequestMapping("show5")
    public String show5(){
        System.out.println("show5");
        //forward 请求转发 和request..getRequestDispatcher("url").forward(request,response)相同
        return "forward:/WEB-INF/pages/success.jsp";
    }
    @RequestMapping("show6")
    public String show6(){
        System.out.println("show6");
        return "redirect:/WEB-INF/pages/success.jsp";
    }
    @RequestMapping("show7")
    public String show7(){
        System.out.println("show7");
        //forward 请求转发 和request..getRequestDispatcher("url").forward(request,response)相同
        return "forward:/test/show3";
    }

}
